<?php


namespace app\common\model\xiluedu;


use app\common\library\Auth;
use think\Exception;
use think\Model;
use function fast\array_get;

class UserCustomMade extends Model
{
    // 表名
    protected $name = 'xiluedu_user_custom_made';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    protected $append = [];

    /**
     *
     */
    public static function add($params){
        $data = array_get($params,'data');
        if(empty($data) || !is_array($data)){
            throw new Exception('参数错误或格式错误');
        }
        $auth = Auth::instance();
        $user_made = [];
        foreach ($data as $row){
            $made_ids = explode(',',$row['made_ids']);
            foreach ($made_ids as $vo){
                $user_made[] = [
                    'user_id'       =>  $auth->id,
                    'made_module_id'=>  $row['made_module_id'],
                    'made_id'       =>  $vo,
                ];
            }
        }
        return (new static)->saveAll($user_made);
    }
}